package com.rabbit.eap.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbit.eap.common.lang.StringUtils;
import com.rabbit.eap.framework.common.api.ApiResult;
import com.rabbit.eap.framework.common.enums.MsgEnum;
import com.rabbit.eap.framework.common.service.impl.BaseServiceImpl;
import com.rabbit.eap.framework.core.pagination.PageInfo;
import com.rabbit.eap.framework.core.pagination.Paging;
import com.rabbit.eap.system.dto.SysDictDataDTO;
import com.rabbit.eap.system.entity.SysDictData;
import com.rabbit.eap.system.mapper.SysDictDataMapper;
import com.rabbit.eap.system.param.SysDictDataPageParam;
import com.rabbit.eap.system.service.ISysDictDataService;
import com.rabbit.eap.system.vo.SysDictDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @projectName: eap
 * @packageName: com.rabbit.eap.system.service.impl
 * @className SysDictDataServiceImpl
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/06/06 23:34:02
 * <p>Description:
 * 字典数据表 服务实现类
 * </p>

 */
@Slf4j
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * @Description: 添加字典数据表
     * @methodName: saveSysDictData
     * @param sysDictDataDTO:sysDictDataDTO数据传输对象
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult saveSysDictData(SysDictDataDTO sysDictDataDTO) throws Exception {
        SysDictData  sysDictData = sysDictDataDTO.convert(SysDictData.class);
        boolean bool = super.save(sysDictData);
        if(bool){
            return ApiResult.ok(MsgEnum.ADD_SUCCESS.getDesc());
        }else{
            return ApiResult.fail(MsgEnum.ADD_FAIL.getDesc());
        }
    }
    /**
     * @Description 修改字典数据表
     * @methodName: modifySysDictData
     * @param  sysDictDataDTO:sysDictDataDTO数据传输对象
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult modifySysDictData(SysDictDataDTO sysDictDataDTO) throws Exception {
        SysDictData  sysDictData = sysDictDataDTO.convert(SysDictData.class);
        boolean bool  =  super.updateById(sysDictData);
        if(bool){
            return ApiResult.ok(MsgEnum.UPDATE_SUCCESS.getDesc());
        }else {
            return ApiResult.fail(MsgEnum.UPDATE_FAIL.getDesc());
        }
    }
    /**
     * @Description 删除字典数据表
     * @methodName: removeSysDictData
     * @param ids:ids集合
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeSysDictData(Long[] ids) throws Exception {
       boolean bool =  super.removeByIds(Arrays.asList(ids));
        if(bool){
            return ApiResult.ok(MsgEnum.DELETE_SUCCESS.getDesc());
        }else {
            return ApiResult.fail(MsgEnum.DELETE_FAIL.getDesc());
        }
    }
    /**
     * @Description 根据Id获取字典数据表详情
     * @methodName: getSysDictDataInfo
     * @param id: id
     * @return:  ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Override
    public ApiResult  getSysDictDataById(Long id) throws Exception {
        if(ObjectUtil.isNull(id)){
            return ApiResult.fail(MsgEnum.PARAM_NOT_NULL.getDesc());
        }
        SysDictData sysDictData = super.getById(id);
        SysDictDataVO sysDictDataVO  = sysDictData.convert(SysDictDataVO.class);
        return ApiResult.ok(sysDictDataVO);
    }

    /**
     * @Description 字典数据表分页列表
     * @methodName: getSysDictDataPageList
     * @param sysDictDataPageParam
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Override
    public ApiResult getSysDictDataPageList(SysDictDataPageParam sysDictDataPageParam) throws Exception {
        Page<SysDictData> page = new PageInfo<>(sysDictDataPageParam, OrderItem.desc("id"));
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(sysDictDataPageParam.getDictLabel()), SysDictData::getDictLabel, sysDictDataPageParam.getDictLabel())
                .eq(ObjectUtil.isNotNull(sysDictDataPageParam.getDictTypeId()), SysDictData::getDictTypeId, sysDictDataPageParam.getDictTypeId());

        IPage<SysDictDataVO> iPage = sysDictDataMapper.selectPage(page, queryWrapper)
                .convert(sysDictData -> sysDictData.convert(SysDictDataVO.class));
        Paging<SysDictDataVO> paging = new Paging<SysDictDataVO>(iPage);
        return ApiResult.ok(paging);
    }
    /**
     * @Description: 根据字典类型查询字典数据信息
     * @methodName: getSysDictDataByDictType
     * @param dictTypeId: 字典类型Id
     * @return: com.rabbit.eap.framework.common.api.ApiResult
     * @date: 2020/6/17 19:15
     * @author: rabbit
     *
     */
    @Override
    public ApiResult getSysDictDataByDictType(Long dictTypeId) {
        QueryWrapper<SysDictData>  queryWrapper=  new QueryWrapper();
        queryWrapper.lambda().eq(SysDictData::getStatus, StringPool.ZERO)
                .eq(SysDictData::getDictTypeId,dictTypeId).orderByAsc(SysDictData::getDictSort);
        List<SysDictData> list  =sysDictDataMapper.selectList(queryWrapper);
        return ApiResult.ok(list);
    }
}
